import React, { Component } from "react";
import { Modal, Tooltip, Form, Table, Row, Col, Spin, Divider, DatePicker, message, Button } from 'antd';
import { findUpdateHistoryList } from "../../../fetch/fetchApi";
import GridTable from '../../../components/display/gridTable';

let isInitPagination = false;
class UpgradeDetails extends Component {
    constructor(props) {
        super(props);
        this.init();
        this.state = {
            gridData: [],
            pagination: {}
        }

    }

    init() {
        this.query = {
            page: '1',
            pagecount: '10'
        };
        this.tableColumns = [
            {
                title: '执行时间',
                dataIndex: 'otaStartTime',
                key: 'otaStartTime'
            }, {
                title: '固件型号',
                dataIndex: 'typeId',
                key: 'typeId',
                className: 'column-nowrap'
            }, {
                title: '升级前版本',
                dataIndex: 'versionPre',
                key: 'versionPre',
            }, {
                title: '升级后版本',
                dataIndex: 'versionAfter',
                key: 'versionAfter',
            }, {
                title: '执行结果',
                dataIndex: 'updateStatusStr',
                key: 'updateStatusStr',
            }, {
                title: '执行人',
                dataIndex: 'updatePerson',
                key: 'updatePerson',
            }
        ];
    }

    findUpdateHistoryList = (params = {}) => {
        this.setState({
            loading: true
        })
        findUpdateHistoryList(this.query).then((res) => {
            const pagination = this.state.pagination;
            pagination.total = res.data.pageLimit.totalCount;
            pagination.current = params.current || 1;
            if (!isInitPagination) {
                isInitPagination = true;
                pagination.onChange = (page, pageSize) => {
                    this.query["page"] = page;
                    this.query["pagecount"] = pageSize;
                    this.findUpdateHistoryList({
                        current: page
                    });
                }
            }
            this.setState({
                gridData: res.data.data,
                loading: false
            });
        });
    }

    componentDidMount() {
        this.query["stubId"] = this.props.stubId;
        this.findUpdateHistoryList();
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.stubId !== this.props.stubId) {
            this.query["page"] = 1;
            this.query["stubId"] = nextProps.stubId;
            this.findUpdateHistoryList();
        }
    }

    render() {
        return (
            <div className="sc-page-content">

                <div className="page-panel">
                    <GridTable postyle="table-center" data={this.state.gridData} loading={this.state.loading} columns={this.tableColumns} pagination={this.state.pagination}/>
                </div>
            </div>
        )
    }
}
export default UpgradeDetails;
